package com.icinfo.cloud.provider.punish.evidence.service.impl;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpException;
import cn.hutool.http.HttpStatus;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.icinfo.cloud.common.core.page.PageResponse;
import com.icinfo.cloud.common.security.util.UserUtil;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.enums.ApiVisitDataSourceEnum;
import com.icinfo.cloud.provider.common.enums.TerritoryCodeEnum;
import com.icinfo.cloud.provider.common.enums.YesOrNoEnum;
import com.icinfo.cloud.provider.common.exception.BusinessException;
import com.icinfo.cloud.provider.common.utils.DateUtil;
import com.icinfo.cloud.provider.common.utils.HeaderUtil;
import com.icinfo.cloud.provider.punish.api.dto.DeptInfoSyncDto;
import com.icinfo.cloud.provider.punish.api.dto.LawEnforcerDto;
import com.icinfo.cloud.provider.punish.api.dto.ThirdAppLoginRequest;
import com.icinfo.cloud.provider.punish.api.enums.TransferEvidenceFileTypeEnum;
import com.icinfo.cloud.provider.punish.approval.constant.AuditStatusConstant;
import com.icinfo.cloud.provider.punish.approval.model.CaseApprovalCompleteRecord;
import com.icinfo.cloud.provider.punish.common.documentoperate.dto.DocumentRelateAttachmentVo;
import com.icinfo.cloud.provider.punish.common.documentoperate.enums.DocumentRecordStatusEnum;
import com.icinfo.cloud.provider.punish.config.model.ConfigBusiParamDocumentInfo;
import com.icinfo.cloud.provider.punish.config.model.SysDepartParam;
import com.icinfo.cloud.provider.punish.csource.manage.model.CaseSourceHandlePersonRecord;
import com.icinfo.cloud.provider.punish.csource.manage.model.CaseSourcePartyInfo;
import com.icinfo.cloud.provider.punish.csource.manage.model.CaseSourceRegisterMainInfo;
import com.icinfo.cloud.provider.punish.evideinv.dto.CaseSourceEventSaveDto;
import com.icinfo.cloud.provider.punish.evideinv.dto.EventReSaveDto;
import com.icinfo.cloud.provider.punish.evideinv.dto.EvidenceButtonUrlQueryDto;
import com.icinfo.cloud.provider.punish.evideinv.dto.QueryEvidencePageDto;
import com.icinfo.cloud.provider.punish.evideinv.enums.EvidenceCertTypeEnum;
import com.icinfo.cloud.provider.punish.evideinv.vo.EvidenceAppraisalDocInfoVo;
import com.icinfo.cloud.provider.punish.evideinv.vo.EvidenceNewSignVo;
import com.icinfo.cloud.provider.punish.evideinv.vo.EvidencePageNewVo;
import com.icinfo.cloud.provider.punish.evidence.service.EvidenceInventoryService;
import com.icinfo.cloud.provider.punish.evidence.vo.EvidenceDetailInfoVo;
import com.icinfo.cloud.provider.punish.evidence.vo.EvidenceFileInfoVo;
import com.icinfo.cloud.provider.punish.execute.dto.ExecuteEventSaveDto;
import com.icinfo.cloud.provider.punish.execute.model.ExecuteHandlePersonRecord;
import com.icinfo.cloud.provider.punish.execute.model.ExecutePartyInfo;
import com.icinfo.cloud.provider.punish.mapper.*;
import com.icinfo.cloud.provider.punish.service.ICasePartiesRecordService;
import com.icinfo.cloud.provider.punish.service.ISysDepartParamService;
import com.icinfo.cloud.provider.punish.service.IXzzfInterfaceService;
import com.icinfo.cloud.provider.punish.ucase.casemain.dto.CaseReasonMainRecordQueryDto;
import com.icinfo.cloud.provider.punish.ucase.casemain.enums.PartyTypeEnum;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.*;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.*;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.enums.CaseStatusChangeTypeEnum;
import com.icinfo.cloud.provider.punish.ucase.punishinform.dto.CriminalCaseTransferQueryDto;
import com.icinfo.cloud.provider.punish.ucase.punishinform.vo.CaseCriminalTransferFileVo;
import com.icinfo.cloud.xzcf.provider.member.userinfo.dto.UserDTO;
import com.icinfo.cloud.xzcf.provider.member.zfpeople.dto.ZfPeopleBaseInfoVO;
import com.icinfo.framework.common.web.Result;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import com.icinfo.framework.tools.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 证据库 新对接
 *
 * @author Administrator
 */
@Service
@Slf4j
public class EvidenceInventoryServiceImpl implements EvidenceInventoryService {

    @Value("${evidenceNew.exceptTzUrl}")
    private String exceptTzUrl;
    @Value("${evidenceNew.api.getEvidenceByCaseId}")
    private String getEvidenceByCaseId;

    private final String CODE = "code";

    private final String DATA = "data";

    private final String DL = "dataList";

    private final Integer TWO_HUNDRED = 200;


    @Resource
    private SysDepartParamMapper sysDepartParamMapper;
    @Resource
    private ISysDepartParamService sysDepartParamService;


    /**
     * 描述：获取证据详情
     *
     * @param caseId 用例id
     * @return {@link List < CaseCriminalTransferFileVo > }
     * @author shishengyao
     * @date 2022/10/11
     */
    @Override
    public List<EvidenceDetailInfoVo> getEvidenceDetailList(String caseId) {
        log.info("getEvidenceByCaseId 调用证据库证据接口 start");
        List<EvidenceDetailInfoVo> list=new ArrayList<>();
        try {
            Map<String, Object> urlParam = new HashMap<>();
            urlParam.put("caseId", caseId);
            log.info("getEvidenceByCaseId -- caseId: {}", caseId);
            String response = HttpUtil.createGet(exceptTzUrl.concat(getEvidenceByCaseId)).form(urlParam).execute().body();
            log.info("getEvidenceByCaseId -- 出参: {}", response);
            Result   result = JSONObject.parseObject(response, Result.class);
            if (HttpStatus.HTTP_OK == result.getCode() && !ObjectUtils.isEmpty(result.getData())) {
                Object data = result.getData();
                if (ObjectUtil.isNotEmpty(data)){
                    list = JSON.parseArray(JSON.toJSONString(data), EvidenceDetailInfoVo.class);
                }
                return list;
            }
        } catch (Exception e) {
            log.error("getEvidenceByCaseId,caseId:{},调用证据库证据接口报错: {}",caseId, e);
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 描述：将证据端信息转换成文书列表格式
     * @param caseId 用例id
     * @return {@link List<CaseDocumentCatalogChainInfoVo> }
     * @author shishengyao
     * @date 2023/01/11
     */
    @Override
    public List<CaseDocumentCatalogChainInfoVo> getEvidenceChainInfo(String caseId) {
        //加入证据信息
        List<CaseDocumentCatalogChainInfoVo> evidenceList = new ArrayList<>();
        List<EvidenceDetailInfoVo> evidenceDetailList = this.getEvidenceDetailList(caseId);
        if (ObjectUtil.isNotEmpty(evidenceDetailList)){
            CaseDocumentCatalogChainInfoVo chainInfoVo = new CaseDocumentCatalogChainInfoVo();
            chainInfoVo.setStageCode("INVESTIGATION");
            chainInfoVo.setStageName("调查取证");
            List<CaseDocumentCatalogChainDetailVo> caseDocumentCatalogChainDetailVoList = new ArrayList<>();
            for (EvidenceDetailInfoVo evidenceDetailInfoVo : evidenceDetailList) {
                CaseDocumentCatalogChainDetailVo detailVo = new CaseDocumentCatalogChainDetailVo();
                detailVo.setDocumentKindName(evidenceDetailInfoVo.getEvidenceName());
                detailVo.setDocumentCreateTime(evidenceDetailInfoVo.getCreateTime());
                detailVo.setDocumentUrl(evidenceDetailInfoVo.getPdfUrl());
                List<EvidenceFileInfoVo> evidenceFileInfoVoList = evidenceDetailInfoVo.getEvidenceFileList();
                //附件
                if (ObjectUtil.isNotEmpty(evidenceFileInfoVoList)) {
                    List<DocumentRelateAttachmentVo> documentRelateAttachmentList = new ArrayList<>();
                    for (EvidenceFileInfoVo fileInfoVo : evidenceFileInfoVoList) {
                        DocumentRelateAttachmentVo attachmentVo = new DocumentRelateAttachmentVo();
                        attachmentVo.setAttachmentId(fileInfoVo.getUid());
                        attachmentVo.setAttachmentTitle(fileInfoVo.getFileName());
                        attachmentVo.setAttachmentType(fileInfoVo.getFileSuffix());
                        attachmentVo.setAttachmentUrl(fileInfoVo.getFileUrl());
                        attachmentVo.setCreateTime(fileInfoVo.getCreateTime());
                        documentRelateAttachmentList.add(attachmentVo);
                    }
                    detailVo.setDocumentRelateAttachmentList(documentRelateAttachmentList);
                }
                caseDocumentCatalogChainDetailVoList.add(detailVo);
            }
            chainInfoVo.setCaseDocumentCatalogChainDetailVoList(caseDocumentCatalogChainDetailVoList);
            evidenceList.add(chainInfoVo);
        }
        return evidenceList;
    }

    public static void main(String[] args) {
        String timestamp = String.valueOf(System.currentTimeMillis());
        System.out.println(DigestUtils.md5DigestAsHex(("c37c14e6a33711ecb6fb0050568f7529".concat("65683b181ac290efc48d47ac9dcb23710959114d").concat(timestamp)).getBytes(StandardCharsets.UTF_8)));
        System.out.println(timestamp);
    }
}
